<div class="modal fade " id="maintainArchivModal"  role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="box box-info">
            <div class="box-header with-border">
                <h3 class="box-title">档案维护</h3>
            </div>
            <div class="form-horizontal" role="form" id="maintainArchivForm"  method="post"  action="/archivmanager/savecontract" enctype="multipart/form-data">
                <div class="box-body">
                    <div class="form-group">
                        <input id="documentId" name="documentId" type="text" style="display:none;">

                        <input id="customerId" name="customerId" type="text" style="display:none;">
                        <label class="col-sm-2 control-label" for="customerNameLabel">客户名称</label>
                        <div class="col-sm-4">
                            <select id="customerName" class=" form-control select2" style="width: 100%">
                                <option value="-1">请选择</option>
                                <#if customerList??>
                                <#list customerList as vo  >
                                <option value="${vo.customerId?if_exists}">${vo.customerName?if_exists}</option>
                                </#list>
                                </#if>
                            </select>
                        </div>

                        <input id="documentType" name="documentType" type="text" style="display:none;">
                        <label class="col-sm-2 control-label" for="documentTypeLabel">档案类型</label>
                        <div class="col-sm-4">
                            <select id="documentTypeList" name="documentTypeList" class=" form-control" style="width: 100%">
                                <option value="-1">请选择</option>
                                <#if documentType??>
                                <#list documentType as vo >
                                <option value="${vo.keyValue?if_exists}">${vo.caption?if_exists}</option>
                                </#list>
                                </#if>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <input id="fileNameText" name="fileNameText" type="text" style="display:none;">
                        <label class="col-sm-2 control-label" for="inputFileLabel">档案文件</label>
                        <div class="col-sm-10">
                            <input type="file" id="file" name="file">
                        </div>
                    </div>
                </div>
            <div class="box-footer">
                <button type="button" class="btn btn-danger pull-left" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary pull-right" onclick="saveArchiv()">保存</button>
            </div>
        </form>
    </div>
</div>
</div>
<#include "../public/js.ftl" >
<#include "../public/css.ftl">
<script src="/js/common.js"></script>

<script>
    $(".select2").select2();

    //初始化页面组件
    function maintainArchivInit() {
        $("#documentId").val("");
        $("#customerName").val(['-1']).trigger('change');
        $("#customerName").removeAttr("disabled");
        $("#documentTypeList").val("-1");
        $("#file").val("");
        $("#fileNameText").val("");
    }

    //修改时设置数据值
    function modifyArchivInit(data) {
        $("#documentId").val(data.documentId);
        $("#customerName").val([data.customerId]).trigger('change');
        $("#customerName").attr("disabled","disabled");
        $("#documentTypeList").val(data.documentType);
        $("#file").val("");
        $("#fileNameText").val(data.fileName);
    }

    //保存处理
    function saveArchiv(){
        //检查客户是否选择
        var customerId = $("#customerName").val();
        if (customerId == "-1" || customerId == "") {
            toastr['error']("请选择客户", '异常');
            return;
        }

        //设置customerId(@RequestParam绑定单个请求参数值，作用于单个参数，所以需要把customerName的值放到customerId)
        $("#customerId").val(customerId);

        //检查合同类型是否选择
        var documentType = $("#documentTypeList").val();
        if (documentType == "-1" || documentType == "") {
            toastr['error']("请选择档案类型", '异常');
            return;
        }

        //档案类型(@RequestParam绑定单个请求参数值，作用于单个参数，所以需要把documentTypeList的值放到documentType)
        $("#documentType").val(documentType);

         var fileNameValue = $("#fileNameText").val();
        //当fileNameValue为空，表示为新增，需检查文件是否为空
        //当fileNameValue不为空，表示为修改，修改时，可不上传文件，只更新信息

        //检查上传文件是否为空
        var filename = $("#file").val();

        if (fileNameValue == "") {
            if (filename == null || filename == undefined || filename == "" || filename.trim() == "") {
                toastr['error']("未选择上传文件", '异常');
                return;
            }
        }

        var length = strlen(filename);
        if (filename.length > 100) {
            toastr['error']("文件名不能超过100个字符","异常");
            return;
        }

        var documentId = $("#documentId").val();

        //上传文件
		var data = {
		    documentId : documentId,
            customerId : customerId,
            documentType : documentType
		};

		fileReq("archivmanager/savearchiv","file",data,function(result){
			if(result.success) {
				toastr['success']('文件上传成功', '提示');
				maintainArchivInit();
				$("#archivTable").bootstrapTable("refresh");
			} else {
				toastr['error'](result.msg, '提示');
			}
		});
    }
</script>
